package com.egg.ylt.fragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.activity.ACT_AddGrowRecord;
import com.umeng.commonsdk.debug.E;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.log.Elog;
import com.yonyou.framework.library.eventbus.EventCenter;

import butterknife.BindView;

/**
 * Created by lijiateng on 2019/1/21.
 */

public class FRA_GrowUpCurve extends BaseFragment {

    public static final String KEY_CURVE_TYPE = "curve_type";
    public static final String KEY_BABY_ID = "baby_id";
    public static final String CURVE_HEIGHT = "h";
    public static final String CURVE_WEIGHT = "w";

    @BindView(R.id.wv_curve)
    WebView wbCurve;

    private String curveType;
    private String babyId;

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {
//        reloadUrl();
        //从三级页面返回调用此处， 使用reload强制不使用缓存，否则会无法刷新
        wbCurve.reload();
        Log.e(TAG, "onUserVisible:1 ");
    }

    @Override
    protected void onUserInvisible() {

    }

    private static final String TAG = "FRA_GrowUpCurve";

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.e(TAG, "onHiddenChanged:2 ");
        if (!hidden) {
//            reloadUrl();
            //同级页面间切换调用此处，使用reload强制不使用缓存，否则会无法刷新
            wbCurve.reload();
        }

    }

    private void reloadUrl() {

        // 清空缓存
        wbCurve.clearCache(true);
        wbCurve.clearHistory();

        //加载h5链接
        String url = String.format(API.BASE_WEB_URL+"/#/test?type=%s&token=%s&babyId=%s",
                curveType, Constants.TOKEN, babyId);
        wbCurve.loadUrl(url);
        Log.e(TAG, "reloadUrl: " + url);
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        curveType = getArguments().getString(KEY_CURVE_TYPE);
        babyId = getArguments().getString(KEY_BABY_ID);

        initWebView();
        reloadUrl();
    }

    private void initWebView() {

        WebSettings settings = wbCurve.getSettings();
        settings.setUseWideViewPort(true);
//        settings.setLoadWithOverviewMode(true);

        wbCurve.getSettings().setJavaScriptEnabled(true);
        wbCurve.addJavascriptInterface(new JSBridge(), "android");
//        wbCurve.getSettings().setSupportMultipleWindows(true);
//        wbCurve.getSettings().setSupportZoom(true);
//        wbCurve.getSettings().setBuiltInZoomControls(true);
//        wbCurve.getSettings().setDisplayZoomControls(false);
//        wbCurve.getSettings().setCacheMode(wbCurve.getSettings().LOAD_NO_CACHE);
//        settings.setDomStorageEnabled(true);
//        settings.setAppCacheEnabled(false);
//        wbCurve.setVerticalScrollBarEnabled(false);

        wbCurve.setWebChromeClient(new WebChromeClient());
        wbCurve.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                String url = String.format("http://test.mumzone.cn:94/#/test?type=%s&token=%s&babyId=%s",
//                        curveType, Constants.TOKEN, babyId);
//                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                toggleShowDialogLoading("加载中...");
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                wbCurve.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                dismissDialogLoad();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_grow_up_curve;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        CookieSyncManager.createInstance(mContext);
        CookieManager instance = CookieManager.getInstance();
        instance.removeAllCookie();
        CookieSyncManager.getInstance().sync();

        wbCurve.setWebViewClient(null);
        wbCurve.setWebChromeClient(null);
        wbCurve.getSettings().setJavaScriptEnabled(false);
        wbCurve.clearCache(true);
    }

    public static FRA_GrowUpCurve newInstance(String type, String babyId) {
        FRA_GrowUpCurve fragment = new FRA_GrowUpCurve();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_CURVE_TYPE, type);
        bundle.putString(KEY_BABY_ID, babyId);
        fragment.setArguments(bundle);
        return fragment;
    }

    class JSBridge {

        @JavascriptInterface
        public void goAddRecord() {
            Bundle bundle = new Bundle();
            bundle.putString(ACT_AddGrowRecord.KEY_BABY_ID, StringUtil.get(babyId));
            readyGo(ACT_AddGrowRecord.class, bundle);
        }

    }

}
